package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

//   @WebInitParam(name = "i",value = "321")
//  int i =321
//  String stuName  ="徐大娃"
//application.setAttribute("i",321);
@WebServlet(urlPatterns = "/initParams"
                    ,initParams = {
                        @WebInitParam(name = "i",value = "321")
                        ,@WebInitParam(name = "stuName",value = "徐大娃")
                        ,@WebInitParam(name = "sex",value = "true")
                    }
)
public class InitParamServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out=resp.getWriter();
        out.print("<h1>Hello my servlet</h1>");

        Enumeration<String> params =this.getServletConfig().getInitParameterNames();
        while (params.hasMoreElements()){
            String paraName = params.nextElement();// i  stuName  sex
            out.append(paraName+ ": "+ this.getServletConfig().getInitParameter(paraName));
        }

        out.append(this.getServletConfig().getInitParameter("stuName"));
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
        //show();
    }

  /*  private void show() {
    }*/
}
